home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / ax25aar.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  5KB  |  198 lines

  1. #include "global.h"
  2. #include "config.h"
  3. #include "mbuf.h"
  4. #include "timer.h"
  5. #include "iface.h"
  6. #include "ax25.h"
  7. #include "icmp.h"
  8. #include "ip.h"
  9. #include "arp.h"
  10. #include "icmp.h"
  11. #include "rip.h"
  12. #include "cmdparse.h"
  13. #include <ctype.h>
  14.  
  15. #ifdef AX25
  16. #ifdef AUTOROUTE
  17.  
  18. int ax25_check_corruption __ARGS((struct ax25 *header));
  19. int ax25_legal              __ARGS((char *call_ssid));
  20. int Ax25_autoroute = 0; /*auto-routing on by default*/
  21.  
  22. #ifdef RSPF
  23. extern int RspfActive;
  24. #endif
  25.  
  26. int
  27. #ifdef PROTOTYPES
  28. doax25autoroute(int argc,char **argv,void *p)
  29. #else
  30. doax25autoroute(argc,argv,p)
  31. int argc ;
  32. char *argv[] ;
  33. void *p;
  34. #endif
  35. {
  36. #ifdef RSPF
  37.     if(RspfActive) {
  38.         tputs("RSPF active. ");
  39.         argc = 0;
  40.     }
  41. #endif
  42.     return setbool(&Ax25_autoroute,"AX.25 IP autorouting",argc,argv);
  43. }
  44.  
  45. /* Create an ARP_RESPONSE packet for any IP packet we hear via an ax25 interface
  46.  * This will automatically update any preexisting entry.
  47.  * If no route is currently defined, then add that as well...
  48.  *
  49.  */
  50. void
  51. #ifdef PROTOTYPES
  52. ax25eavesdrop(struct iface *iface,char *source,char *dest,struct mbuf *bp)
  53. #else
  54. ax25eavesdrop(iface,source,dest,bp)
  55. struct iface *iface;
  56. char *source;
  57. char *dest;
  58. struct mbuf *bp;
  59. #endif
  60. {
  61.     struct ip ipstuff;
  62.     register struct route *rt;
  63.     struct arp_tab *ap;
  64.     int16 ip_len;
  65.     char (*mpp)[AXALEN];
  66.     
  67.     if(len_p(bp) < IPLEN){
  68.         /* The packet is shorter than a legal IP header */
  69.         return;
  70.     }
  71.     /* Sneak a peek at the IP header's IHL field to find its length */
  72.     ip_len = (bp->data[0] & 0xf) << 2;
  73.     if(ip_len < IPLEN){
  74.         /* The IP header length field is too small */
  75.         return;
  76.     }
  77.     if(cksum(NULLHEADER,bp,ip_len) != 0){
  78.         /* Bad IP header checksum; discard */
  79.         return;
  80.     }
  81.     if(ntohip(&ipstuff,&bp) == -1){
  82.         return;
  83.     }
  84.  
  85.     Arp_stat.recv++;
  86.  
  87.     for(mpp = Ax25multi;(*mpp)[0] != '\0';mpp++){
  88.         if(addreq(source,*mpp)){
  89.             /* This guy is trying to say he's got the broadcast address! */
  90.             Arp_stat.badaddr++;
  91.             return;
  92.         }
  93.     }
  94.  
  95.  
  96. /* If this entry already exists, but points to a different iface, don't add. */
  97.     if((rt = rt_lookup(ipstuff.source)) != NULLROUTE)
  98.         if(rt != &R_default)
  99.             if(rt->iface != iface)
  100.                 return;
  101.  
  102. /* If no route currently exists, add one with a high metric, ttl = ARPLIFE, mark it as private */
  103. /* If we know a better route, or we have a manual route entered, go home. */
  104.     if(    rt == NULLROUTE ||
  105.         rt == &R_default ||
  106.         ((rt->timer.duration != 0) && (rt->metric >= (RIP_INFINITY - 1))))
  107.             rt_add(ipstuff.source, (unsigned int)32, (int32)0, iface, RIP_INFINITY - 1, ARPLIFE, 1);
  108.  
  109. /* If route is via a gateway, do not add an ARP entry. */
  110.     if( rt->gateway == (int32)0){
  111.         /* If this guy is already in the table, update its entry
  112.          * unless it's a manual entry (noted by the lack of a timer)
  113.          */
  114.         ap = NULLARP;    /* ap plays the role of merge_flag in the spec */
  115.         if(((ap = arp_lookup(ARP_AX25,ipstuff.source,iface)) != NULLARP
  116.             && ap->timer.duration != 0) || ap == NULLARP){
  117.                 ap = arp_add(ipstuff.source,ARP_AX25,source,0,iface);
  118.         }
  119.     }
  120.     return;
  121. }
  122.  
  123. /* Inspect the AX.25 header to determine if it is legal.  If it has anything
  124.     wrong with it, dump this packet...
  125. */
  126. #define    ishash(x)    ((char)(x) == '#' ? 1 : 0)
  127. #define    isaspace(x)    ((char)(x) == ' ' ? 1 : 0)
  128.  
  129. int ax25_check_corruption(header)
  130. struct ax25 *header;
  131. {
  132.     int loop;
  133.  
  134.     /* The header structure contains two fixed-length and one variable-length
  135.         char arrays, containing the ax25 callsign/ssid pairs for the source,
  136.         destination, and (optional) digipeaters.
  137.  
  138.         The integer "ndigis" says how many digis are in the path, upto the
  139.         constant MAXDIGIS.
  140.  
  141.         Because the arrays are not null-terminated, we can just walk through
  142.         the char arrays looking for any illegal characters to determine if
  143.         the header is corrupt.  We'll rely on the higher-level protocols to
  144.         determine if the data in the packet is corrupt...
  145.  
  146.         We return 0 if the packet is possibly good, or 1 if it is definitely
  147.         corrupt.
  148.     */
  149.  
  150.     if (header->ndigis > MAXDIGIS || header->ndigis < 0){
  151.         return 1;
  152.     }
  153.  
  154.     if (ax25_legal(header->source)){
  155.         return 1;
  156.     }
  157.  
  158.     if (ax25_legal(header->dest)){
  159.         return 1;
  160.     }
  161.  
  162.     for (loop = 0; loop < header->ndigis; loop++)
  163.         if (ax25_legal(header->digis[loop])){
  164.             return 1;
  165.         }
  166.  
  167.     return 0;
  168.  
  169.  
  170. }
  171.  
  172. /* Check the callsign portions. */
  173. int ax25_legal(call_ssid)
  174. char *call_ssid;
  175. {
  176.     int loop;
  177.     unsigned char chr;
  178.  
  179.     for (loop = 0; loop < AXALEN-1; loop++){
  180.  
  181.         /* Put the ascii value of the char into chr */
  182.         chr = (*(call_ssid + loop));
  183.         chr = chr >> 1;
  184.  
  185.         /* Now see if it valid. */
  186.         if ((isupper(chr) | isdigit(chr) | ishash(chr) | isaspace(chr)) == 0)
  187.          /* A bad chr */
  188.             return 1;
  189.     }
  190.  
  191.     /* The header passed inspection, so return 0... */
  192.     return 0;
  193.  
  194. }
  195.  
  196. #endif /* AUTOROUTE */
  197. #endif /* AX25 */
  198.